/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.param.PayParam;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.pay.PayInfoDto;
import com.yami.shop.common.enums.PayType;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.PayManagerService;
import com.yami.shop.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/p/order")
@Api(tags = "订单接口")
@AllArgsConstructor
public class PayController {

    private final PayService payService;
    private final PayManagerService payManagerService;
    private final OrderService orderService;

    /**
     * 支付接口
     */
    @PostMapping("/pay")
    @ApiOperation(value = "根据订单号进行支付", notes = "根据订单号进行支付")
    @SneakyThrows
    public ResponseEntity<?> pay(HttpServletResponse httpResponse,@Valid @RequestBody PayParam payParam) {
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();

        if (!user.isEnabled()) {
            // 您已被禁用，不能购买，请联系平台客服
            throw new YamiShopBindException("yami.order.pay.user.disable");
        }


        PayInfoDto payInfo = payService.pay(userId, payParam);

        payInfo.setBizUserId(user.getBizUserId());
        payInfo.setPayType(payParam.getPayType());
        payInfo.setApiNoticeUrl("/notice/pay/order/" + payParam.getPayType());
        payInfo.setReturnUrl(payParam.getReturnUrl());
        if (payInfo.getPayType().equals(PayType.BALANCE.value())){
            payInfo.setUserId(userId);
            payInfo.setType(1);
        }

//        // 仅在测试支付回调的时候可以打开
//        payService.paySuccess(payInfo.getPayNo(),"");

        return payManagerService.doPay(httpResponse, payInfo);
    }

    @GetMapping("/isPay/{orderNumbers}")
    @ApiOperation(value = "根据订单号查询该订单是否已经支付", notes = "根据订单号查询该订单是否已经支付")
    public ResponseEntity<Boolean> isPay(@PathVariable String orderNumbers) {
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();

        String[] orderNumberArr = orderNumbers.split(StrUtil.COMMA);
        String orderNumber = orderNumberArr[0];
        int count = orderService.count(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, orderNumber).eq(Order::getUserId, userId).eq(Order::getIsPayed, 1));
        return ResponseEntity.ok(count > 0);
    }
}
